<?php
  function grandTotalCompare($a, $b)
  {
    if ($a['points'] == $b['points'] &&
        $a['total'] == $b['total'])
      return 0;

    if ($a['points'] < $b['points'])
      return 1;

    if ($a['points'] == $b['points'] &&
        $a['total'] < $b['total'])
      return 1;

    return -1;
  }

  //Mount classification table
  $grandTotalByTeam = array();
  foreach ($teams as $id => $team)
  {
    $data = array();
    $data['points'] = 0;
    $data['total']  = 0;
    $grandTotalByTeam[$id] = $data;
  }

  // Classify by game
  $game_results = array();
  foreach ($games as $game_id => $game)
  {
    $totalByTeam = array();

    // Initialize totals
    foreach ($game['teams'] as $team_id) $totalByTeam[$team_id] = 0;

    // Calculate totals
    foreach ($game['points'] as $player_id => $point)
    {
      if ($point['summed'] == 'true')
      {
        $team_id = $players[$player_id]['team_id'];
        $totalByTeam[$team_id] += $point['sum'];
      }
    }
    
    // Sort by totals
    $totals = array_unique($totalByTeam);
    rsort($totals);

    // Get totals amount
    $totalsCount = array_count_values($totalByTeam);

    // Calculate points by total
    $pointsByTotal = array();
    $position = 0;
    foreach ($totals as $total)
    {
      $totalPoints = 0;
      $totalPosition = $position + 1;
      for ($i=0; $i<$totalsCount[$total]; $i++)
        $totalPoints += $points[++$position];
      $pointsByTotal[$total] = array();
      $pointsByTotal[$total]['points']   = $totalPoints / $totalsCount[$total];
      $pointsByTotal[$total]['position'] = $totalPosition;
    }

    // Sum points and totals for each team grand total and save game result
    $game_results[$game_id] = array();
    foreach ($totalByTeam as $team_id => $total)
    {
      $grandTotalByTeam[$team_id]['points'] += $pointsByTotal[$total]['points'];
      $grandTotalByTeam[$team_id]['total']  += $total;
      
      $game_results[$game_id][$team_id] = array();
      $game_results[$game_id][$team_id]['position'] = $pointsByTotal[$total]['position'];
      $game_results[$game_id][$team_id]['points']   = $pointsByTotal[$total]['points'];
      $game_results[$game_id][$team_id]['total']    = $total;
    }
  }

  // Order grand total classification
  uasort($grandTotalByTeam, 'grandTotalCompare');
?>

<table cellspacing="0" class="classificationTable">
  <tr>
    <th>Posição</th>
    <th>Equipe</th>
<?php foreach ($games as $game): ?>
    <th><?php echo $game['place'] ?></th>
<?php endforeach; ?>
    <th>Total</th>
  </tr>

<?php $position = 0 ?>
<?php foreach ($grandTotalByTeam as $team_id => $grandTotal): ?>
  <tr>
    <td class="classificationTablePosition"><?php echo ++$position ?></td>
    <td class="classificationTableTeam"><?php echo $teams[$team_id]['name'] ?></td>
  <?php foreach ($games as $game_id => $game): ?>
    <?php if ($game_results[$game_id][$team_id]['position'] == 1): ?>
    <td class="classificationTableHighlightedGame">
    <?php else: ?>
    <td class="classificationTableGame">
    <?php endif; ?>
    <?php echo $game_results[$game_id][$team_id]['total'] ?>
      <div class="classificationTableTotalPoints"><?php echo $game_results[$game_id][$team_id]['points'] ?></div>
    </td>
  <?php endforeach; ?>
    <td class="classificationTableTotal"><?php echo $grandTotalByTeam[$team_id]['total'] ?><br/>
      <div class="classificationTableTotalPoints"><?php echo $grandTotalByTeam[$team_id]['points'] ?></div>
    </td>
  </tr>
<?php endforeach; ?>
</table>
